import http from '../../utils/request'
Page({
    /**
     * 页面的初始数据
     */
    data: {
        callIshow:false,
        nianActive:0,
        xuekeActive:0,
        nianjiinfo:'',
        xuekeinfo:'',
        hobbyIshow:false,
        isShow:false,
        userInfo:null,
        intergal:'',
        sessionInfo:'',
        phonenumber:'',
        linkadress:''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getxueDuan()
        this.getxueKe(11739)
        //判断是否登录
        let userInfo = wx.getStorageSync('userInfo') || null
        let opses = wx.getStorageSync('userInfo')?JSON.parse(wx.getStorageSync('third_Session')):null
        this.setData({userInfo:JSON.parse(userInfo)})
        this.setData({sessionInfo:opses})
        if(wx.getStorageSync('userInfo')){
            this.getMyIntegral1()
        }
    },
    //登录
    loginbutton(){
        let pages = getCurrentPages();
        let currPage = null;
        if (pages.length) {
            currPage = pages[pages.length - 1];
        }
        var site = currPage.route.lastIndexOf("\/");
        //截取最后一个/后的值
        let name = currPage.route.substring(site + 1, currPage.route.length);
        this.setData({isShow:true,linkadress:name})
    },
    //实时获取积分
    async getMyIntegral1(){
        let that = this
        let options={
                url:'/getMyIntegral',
                method:'POST',
                data:{userId:wx.getStorageSync('userInfo')?JSON.parse(wx.getStorageSync('userInfo')).id:null,authorization:wx.getStorageSync('userInfo')?JSON.parse(wx.getStorageSync('userInfo')).authorization:null},
                header:{"content-type":"application/json;charset=UTF-8"}
            }
        let res = await http(options)
        if(res.data.code==0){
            that.setData({intergal:res.data.obj})
        }else{
            if(res.data.code==5){
                that.setData({isShow:true})
            }else{
                wx.showToast({
                    title: res.data.msg,
                    icon: 'error',
                    duration: 2000
                })
            }
        }
        
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        //判断是否登录
        let userInfo = wx.getStorageSync('userInfo') || null
        let opses = wx.getStorageSync('userInfo')?JSON.parse(wx.getStorageSync('third_Session')):null
        this.setData({userInfo:JSON.parse(userInfo)})
        this.setData({sessionInfo:opses})
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    //获取学段
    async getxueDuan(){
        let that = this
        let options={
                url:'/getNavs5',
                method:'POST',
                data:{id:0},
                header:{"content-type":"application/json;charset=UTF-8"}
            }
        let res = await http(options)
        that.setData({nianji:res.data})
    },
    //获取学科
    async getxueKe(id){
        let that = this
        let options={
                url:'/getNavs5',
                method:'POST',
                data:{id:id},
                header:{"content-type":"application/json;charset=UTF-8"}
            }
        let res = await http(options)
        that.setData({xuke:res.data})
    },
    //所属年级
    nianjiClick(e){
        let id = e.currentTarget.dataset.id
        let index = e.currentTarget.dataset.index
        let name = e.currentTarget.dataset.name
        this.setData({nianActive:index,nianjiinfo:name})
        this.getxueKe(id)
    },
    //所属学科
    xuekeClick(e){
        let index = e.currentTarget.dataset.index
        let name = e.currentTarget.dataset.name
        this.setData({xuekeActive:index,xuekeinfo:name})
    },
    // 客服二维码
    kefuclass(){
        wx.navigateTo({
            url: '/pages/applyconsult/applyconsult',
        })
        // this.setData({callIshow:true})
    },
    //关闭客服二维码
    kefuCode(){
        this.setData({callIshow:false})
    },
    //显示学科学段
    gaozhongishow(){
        this.setData({hobbyIshow:true})
    },
    //确认取消
    cancel(){
        this.setData({hobbyIshow:false})
    },
    //我的积分
    rechargeLink(){
        if(this.data.userInfo){
            wx.navigateTo({
                url: '/pages/recharge/recharge',
            })
        }else{
            this.setData({isShow:true})
        } 
    },
    //个人信息
    infomation(){
        wx.navigateTo({
            url: '/pages/infomation/infomation',
          })
    },
    //消息通知
    noticeLink(){
        wx.navigateTo({
            url: '/pages/notice/notice',
        })
    },
    //常见问题
    questionLink(){
        wx.navigateTo({
            url: '/pages/question/question',
        })
    },
    // 我的下载
    downloadLink(){
        wx.navigateTo({
            url: '/pages/mydown/mydown',
        })
    },
    // 我的上传
    uploadLink(){
        wx.navigateTo({
            url: '/pages/upload/upload',
        })
    },
    //关于我们
    aboutLink(){
        wx.navigateTo({
            url:'/pages/webview/webview?url=https://www.bslmedu.com/con/#/index',
        })
    },
    //绑定手机号
    async getPhoneNumber (e) {
        let {iv,encryptedData} = e.detail
        let {userInfo} = this.data
        // let encryptedData = sessionInfo.encryptedData
        // let iv = sessionInfo.iv
        let sessionkey = userInfo.session_key
        let openId = userInfo.openId
        let that = this
        let options={
                url:'/authPhone',
                method:'POST',
                data:{encryptedData:encryptedData,session_key:sessionkey,iv:iv,openid:openId},
                header:{"content-type":"application/json;charset=UTF-8"}
            }
        let res = await http(options)
        wx.setStorageSync('userInfo', JSON.stringify(res.data.obj))
       this.onLoad()
    },
    quitLogin(){
        let that = this
        wx.clearStorage({
            success:()=>{
                wx.showToast({
                  title: '退出成功',
                  icon:'success',
                  duration: 2000,
                  success:function(){
                    setTimeout(function () {
                        that.onLoad()
                    }, 2000) //延迟时间
                  }
                })
               
            }
        })
    }
})